package ru.amse.tsyganov.jumleditor.view;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import ru.amse.tsyganov.jumleditor.graphicaleditor.ItemsSelectionListener;
import ru.amse.tsyganov.jumleditor.model.common.Edge;
import ru.amse.tsyganov.jumleditor.model.common.State;
import ru.amse.tsyganov.jumleditor.view.common.EdgeView;
import ru.amse.tsyganov.jumleditor.view.common.StateView;

public class SelectedItems {
	
	private final Set<ItemsSelectionListener> itemsSelectionListeners = 
		new HashSet<ItemsSelectionListener>();
	
	private final Set<StateView<? extends State>> selectedStates = 
		new LinkedHashSet<StateView<? extends State>>();
	
	private final Set<EdgeView<? extends Edge>> selectedTransitions = 
		new LinkedHashSet<EdgeView<? extends Edge>>();

	public boolean addSelectionListener(ItemsSelectionListener listener) {
		return itemsSelectionListeners.add(listener);
	}
	
	public boolean removeSelectionListener(ItemsSelectionListener listener) {
		return itemsSelectionListeners.remove(listener);
	}
	
	private void fireSelectionChanged() {
		for (ItemsSelectionListener listener : itemsSelectionListeners) {
			listener.selectedItemsChanged(this);
		}
	}
	
	public boolean selectedStatesAdd(
			StateView<? extends State> stateView) {
		
		boolean result = selectedStates.add(stateView);
		if (result) {
			fireSelectionChanged();
		}
		return result;
	}
	
	public boolean selectedStatesRemove(
			StateView<? extends State> stateView) {
		
		boolean result = selectedStates.remove(stateView);
		if (result) {
			fireSelectionChanged();
		}
		return result;
	}
	
	public boolean selectedStatesContains(
			StateView<? extends State> stateView) {
		
		return selectedStates.contains(stateView);
	}
	
	public int selectedStatesSize() {
		return selectedStates.size();
	}
	
	public void selectedStatesClear() {
		if (selectedStates.size() > 0) {
			selectedStates.clear();
			fireSelectionChanged();
		}
	}
	
	public Collection<StateView<? extends State>> getSelectedStates() {
		return selectedStates;
	}
	
	public boolean selectedTransitionsAdd(
			EdgeView<? extends Edge> transitionView) {
		
		boolean result = selectedTransitions.add(transitionView);
		if (result) {
			fireSelectionChanged();
		}
		return result;
	}
	
	public boolean selectedTransitionsRemove(
			EdgeView<? extends Edge> transitionView) {
		
		boolean result = selectedTransitions.remove(transitionView);
		if (result) {
			fireSelectionChanged();
		}
		return result;
	}
	
	public boolean selectedTransitionsContains(
			EdgeView<? extends Edge> transitionView) {
		
		return selectedTransitions.contains(transitionView);
	}
	
	public int selectedTransitionsSize() {
		return selectedTransitions.size();
	}
	
	public void selectedTransitionsClear() {
		if (selectedTransitions.size() > 0) {
			selectedTransitions.clear();
			fireSelectionChanged();
		}
	}

	public Collection<EdgeView<? extends Edge>> getSelectedTransitions() {
		return selectedTransitions;
	}
	
	public void addAllStates(
			Collection<? extends StateView<? extends State>> c) {
		selectedStates.addAll(c);
	}
	
	public void addAllTransitions(
			Collection<? extends EdgeView<? extends Edge>> c) {
		selectedTransitions.addAll(c);
	}
}
